﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DemoObject;
using BusinessObjects;

public partial class Admin_Module_Album_Controls_AlbumList : System.Web.UI.UserControl
{
    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";
    private const int PageSize = 20;

    private string GridViewSortDirection
    {

        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = "ASC";
            return ViewState["sortDirection"].ToString();
        }
        set { ViewState["sortDirection"] = value; }

    }
    public int PageIndex
    {
        get
        {
            if (Request["page"] != null)
                return int.Parse(Request["page"].ToString());
            return 1;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData("");
        }

    }

    private void BindData(string strSort)
    {
        HashSet<string> hs = new HashSet<string>();
        hs.Add(strSort);
        if (Request["KeyWord"] != null)
            txtName.Text = Request["KeyWord"];
        bool? IsVisible = GetIsVisibleBool();
        switch (IsVisible)
        {
            case true:
                cbIsVisible.Checked = true;
                cbIsVisible1.Checked = false;
                break;
            case false:
                cbIsVisible.Checked = false;
                cbIsVisible1.Checked = true;
                break;
            default:
                cbIsVisible.Checked = true;
                cbIsVisible1.Checked = true;
                break;
        }

        int RowCount = 0;
        grv.DataSource = TblAlbumRepository.CreateInstant().SelectListItem(null, "", txtName.Text, IsVisible, null, PageIndex, PageSize, out RowCount, hs);
        grv.DataBind();
        smartPager.PageSize = PageSize;
        smartPager.QueryStringKey = "page";
        smartPager.RowCount = RowCount;
        smartPager.UrlFormatString = "/Admin/Module/Album/AlbumList.aspx?KeyWord" + txtName.Text + "&IsVisible=" + GetIsVisible() + "&page={0}";
        smartPager.Visible = RowCount > PageSize;

    }
    protected void grv_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (GridViewSortDirection.Equals("ASC"))
            GridViewSortDirection = "DESC";
        else
            GridViewSortDirection = "ASC";
        string strSort = e.SortExpression + " " + GridViewSortDirection;
        BindData(strSort);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/Module/Album/AlbumList.aspx?KeyWord=" + txtName.Text + "&IsVisible=" + GetIsVisible());
    }
    protected void grv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("DeleteItem"))
        {
            HiddenField hdfId = (HiddenField)grv.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("hdfId");
            string Whereclause = "";
            string Whereclause1 = " Id='" + hdfId.Value + "'";
            string Whereclause2 = "And Id<>'" + hdfId.Value + "'";
            try
            {
                TblAlbum obj = TblAlbumRepository.CreateInstant().SelectOne(new Guid(hdfId.Value));
                System.IO.File.Delete(Server.MapPath(obj.ImageUrl));
            }
            catch
            {
                Trace.Write("Loai xoa hinh");
            }
            UpdateOrder.UpdateOrders("TblAlbum", Whereclause, Whereclause1, Whereclause2, 100);
            TblAlbumRepository.CreateInstant().Delete(new Guid(hdfId.Value));
            Response.Redirect(Request.RawUrl);
        }
    }
    protected void btnUpdateOrder_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < grv.Rows.Count; i++)
        {
            TextBox txtOrder = (TextBox)grv.Rows[i].FindControl("txtOrder");
            HiddenField hdfOrder = (HiddenField)grv.Rows[i].FindControl("hdfOrder");
            HiddenField hdfId = (HiddenField)grv.Rows[i].FindControl("hdfId");
            int OldIndex = int.Parse(hdfOrder.Value);
            int NewIndex = int.Parse(txtOrder.Text);

            string Whereclause = "";
            string Whereclause1 = " Id='" + hdfId.Value + "'";
            string Whereclause2 = " And Id<>'" + hdfId.Value + "'";
            if (OldIndex != NewIndex)
                UpdateOrder.UpdateOrders("TblAlbum", Whereclause, Whereclause1, Whereclause2, NewIndex);
        }
        Response.Redirect(Request.RawUrl);
    }
    private int GetIsVisible()
    {
        int IsVisible = 0;
        if (cbIsVisible.Checked && !cbIsVisible1.Checked)
            IsVisible = 1;
        else if (!cbIsVisible.Checked && cbIsVisible1.Checked)
            IsVisible = -1;
        return IsVisible;
    }
    private bool? GetIsVisibleBool()
    {
        bool? IsVisible = null;
        if (Request["IsVisible"] != null)
            switch (Request["IsVisible"])
            {
                case "1":
                    IsVisible = true;
                    break;
                case "-1":
                    IsVisible = false;
                    break;
                default:
                    IsVisible = null;
                    break;
            }
        return IsVisible;
    }
}
